/*
 * Copyright (C) 2012 DataSymphony (http://datasymphony.com.au)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package au.com.ds.restty.js;

import java.util.LinkedHashMap;
import java.util.Map;

public class JsObject extends JsAnyObject {
	private LinkedHashMap<JsAnyObject, JsAnyObject> mChildren = new LinkedHashMap<JsAnyObject, JsAnyObject>();

	protected JsObject() {
		super(JsObjectType.OBJECT);
	}

	protected JsObject(Map<JsAnyObject, JsAnyObject> map) {
		super(JsObjectType.OBJECT);
		if (map != null){
			mChildren.putAll(map);
		}
	}

	public Map<JsAnyObject, JsAnyObject> getValue(){
		return mChildren;
	}
	
	public int getSize(){
		return mChildren.size();
	}
	
	public JsAnyObject get(JsAnyObject key){
		JsAnyObject result = mChildren.get(key);
		return result == null ? JsObjectFactory.getJsUndefined() : result;
	}
	
	public JsAnyObject get(String key){
		return get(JsObjectFactory.getJsString(key));
	}
	
	public void put(JsAnyObject key, JsAnyObject value){
		mChildren.put(key, value);
	}
	
	public void put(String key, JsAnyObject value){
		put(JsObjectFactory.getJsString(key), value);
	}

	@Override
	public int hashCode() {
		return mChildren.hashCode();
	}

	@Override
	public boolean equals(Object obj) {
		if (obj == this){
			return true;
		}

		if (obj == null || !(obj instanceof JsObject)){
			return false;
		}
		
		JsObject jso = (JsObject) obj;

		return mChildren.equals(jso.mChildren);
	}

	@Override
	public String toString() {
		return mChildren.toString();
	}
}
